/**
 *  @author: zhangxt
 *  @date:2023/02/28
 *  @description: 请求统一处理
 */

import service from '@/api/http'
import { queryParams } from '@/utils/util'
// 请求统一处理
export async function request(param, config) {
  // 拼接query参数
  if (['get', 'GET', 'delete', 'DELETE'].includes(param.method) && param.query) {
    param.url += queryParams(param.query)
  }
  // loading、toast处理
  if (config) {
    const { loading = false, msg = false } = config
    // 开启遮罩层
    if (loading) {
      console.log('开启遮罩层')
    }
    let result = {}
    await service(param).then(res => {
      result = res
      // 关闭遮罩层
      if (loading) {
        console.log('关闭遮罩层')
      }
      // 统一消息处理
      if (msg) {
        console.log('统一消息处理')
      }
    })
    return Promise.resolve(result)
  } else {
    return service(param)
  }
}
